<?php

/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Sitealbum
 * @copyright  Copyright 2010-2011 BigStep Technologies Pvt. Ltd.
 * @license    http://www.socialengineaddons.com/license/
 * @version    $Id: install.php 2011-08-026 9:40:21Z SocialEngineAddOns $
 * @author     SocialEngineAddOns
 */
class Sitealbum_Installer extends Engine_Package_Installer_Module {

  function onPreinstall() {

    $db = $this->getDb();

    $select = new Zend_Db_Select($db);

    $select
            ->from('engine4_core_modules')
            ->where('name = ?', 'album')
            ->limit(1);

    $checkModule = $select->query()->fetch();

    if (!empty($checkModule)) {
      $PRODUCT_TYPE = 'sitealbum';
      $PLUGIN_TITLE = 'Sitealbum';
      $PLUGIN_VERSION = '4.2.6';
      $PLUGIN_CATEGORY = 'plugin';
      $PRODUCT_DESCRIPTION = 'Advanced Photo Albums Plugin';
      $_PRODUCT_FINAL_FILE = 0;
      $SocialEngineAddOns_version = '4.2.6';
      $PRODUCT_TITLE = 'Advanced Photo Albums';
      $file_path = APPLICATION_PATH . "/application/modules/$PLUGIN_TITLE/controllers/license/ilicense.php";
      $is_file = file_exists($file_path);
      if (empty($is_file)) {
        include_once APPLICATION_PATH . "/application/modules/Sitealbum/controllers/license/license3.php";
      } else {
        include_once $file_path;
      }
      parent::onPreinstall();
    } else {
      $base_url = Zend_Controller_Front::getInstance()->getBaseUrl();
      return $this->_error("<span style='color:red'>Note: You have not installed the Album Plugin on your site. Please first install Album Plugin on your site and then install this Plugin.</span><br/> <a href='" . $base_url . "/manage'>Click here</a> to go to Manage Packages.");
    }
  }

  function onInstall() {
    $db = $this->getDb();
    $table_exist = $db->query('SHOW TABLES LIKE \'engine4_album_photos\'')->fetch();
    if (!empty($table_exist)) {
      $column_exist = $db->query('SHOW COLUMNS FROM engine4_album_photos LIKE \'photo_hide\'')->fetch();
      if (empty($column_exist)) {
        $db->query('ALTER TABLE `engine4_album_photos` ADD `photo_hide` BOOL NOT NULL DEFAULT "0";');
      }
    }

    $db->query("UPDATE  `engine4_seaocores` SET  `is_activate` =  '1' WHERE  `engine4_seaocores`.`module_name` ='sitealbum';");

    //START CODE FOR LIGHTBOX
    //HERE WE CHECKING THAT SITEALBUM ENTRY EXIST IN THE CORE MODULE TABLE OR NOT
//    $select = new Zend_Db_Select($db);
//    $select
//            ->from('engine4_core_modules', array('version'))
//            ->where("name =?", "sitealbum");
//    $sitealbumVersion = $select->query()->fetchAll();
    //IF NOT EXIST THEN WE INSERTING THE LIGHTBOX SHOULD BE DISPLAY OR NOT
    //if (empty($sitealbumVersion)) {
//    $value = '';
//    $select = new Zend_Db_Select($db);
//    $value = $select
//            ->from('engine4_core_settings', array('value'))
//            ->where("name =?", "socialengineaddon.display.lightbox")
//            ->query()
//            ->fetchColumn();

//    //IF LIGHTBOX IS NOT DISPLAY THEN WE WILL INSERTING THE ACTIVITY FEED VALUE
//    if (empty($value)) {
//      $select = new Zend_Db_Select($db);
//      $select
//              ->from('engine4_core_settings', array('name'))
//              ->where("name Like ?", "%socialengineaddon.lightbox.option.display%");
//      $name = $select->query()->fetchAll();
//
//      $count = count($name);
//
//      $select = new Zend_Db_Select($db);
//      $select
//              ->from('engine4_core_settings', array('name'))
//              ->where("value =?", "activity");
//      $name = $select->query()->fetchColumn();
//      if (empty($name)) {
//        $name = 'socialengineaddon.lightbox.option.display.' . ++$count;
//        $db->insert('engine4_core_settings', array(
//            'name' => $name,
//            'value' => 'activity'
//        ));
//      }
//
//      $select = new Zend_Db_Select($db);
//      $select
//              ->from('engine4_core_settings', array('name'))
//              ->where("value =?", "group");
//      $name = $select->query()->fetchColumn();
//      if (empty($name)) {
//        $name = 'socialengineaddon.lightbox.option.display.' . ++$count;
//        $db->insert('engine4_core_settings', array(
//            'name' => $name,
//            'value' => 'group'
//        ));
//      }
//
//      $select = new Zend_Db_Select($db);
//      $select
//              ->from('engine4_core_settings', array('name'))
//              ->where("value =?", "event");
//      $name = $select->query()->fetchColumn();
//      if (empty($name)) {
//        $name = 'socialengineaddon.lightbox.option.display.' . ++$count;
//        $db->insert('engine4_core_settings', array(
//            'name' => $name,
//            'value' => 'event'
//        ));
//      }
//      //}
//    }
//
//    //END CODE FOR LIGHTBOX

    parent::onInstall();
  }

  function onEnable() {
    $this->updateAlubmsConfiguration("0");
    parent::onEnable();
  }

  function onDisable() {
    $this->updateAlubmsConfiguration("1");
    parent::onDisable();
  }

  public function updateAlubmsConfiguration($enable) {
    $db = $this->getDb();
    $db->query("UPDATE `engine4_core_menuitems` SET `enabled` = '$enable' WHERE `engine4_core_menuitems`.`name` ='core_main_album' LIMIT 1 ;");
    $db->query("UPDATE `engine4_core_menuitems` SET `enabled` = '$enable' WHERE `engine4_core_menuitems`.`name` ='core_sitemap_album' LIMIT 1 ;");
    $db->query("UPDATE `engine4_core_menuitems` SET `enabled` = '$enable' WHERE `engine4_core_menuitems`.`name` ='mobi_browse_album' LIMIT 1 ;");
  }

}

?>
